package com.architecture.core.service.query;

import com.architecture.core.entity.Category;
import com.architecture.core.except.ErrException;
import com.architecture.core.service.AbstractService;
import com.architecture.core.utils.ConfigUtil;
import com.architecture.core.utils.ErrCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: aguang
 * Date: 13-1-21
 * Time: 下午6:13
 * To change this template use File | Settings | File Templates.
 */
@Service("queryCategoryService")
public class QueryCategoryService extends AbstractService {

    public List<Category> findCategorysByGroupId(String groupId) throws ErrException {
        String ql = ConfigUtil.getString(ConfigUtil.D_QUERY_CATEGORY_BY_G);
        if(StringUtils.isBlank(ql)){
            throw new ErrException(ErrCode.BLANK_STR);
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("groupId", groupId);
        try {
            return super.baseDAO.find(ql, paramMap);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Category> findCategorysByGroupId(String groupId, String userId) throws ErrException {
        String ql = ConfigUtil.getString(ConfigUtil.D_QUERY_CATEGORY_BY_G_U);
        if(StringUtils.isBlank(ql)){
            throw new ErrException(ErrCode.BLANK_STR);
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("groupId", groupId);
        paramMap.put("userId", userId);
        try {
            return super.baseDAO.find(ql, paramMap);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Category> findCategorysByUserId(String userId) throws ErrException {
        String ql =ConfigUtil.getString(ConfigUtil.D_QUERY_CATEGORY_BY_U);
        if(StringUtils.isBlank(ql)){
            throw new ErrException(ErrCode.BLANK_STR);
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("userId", userId);
        try {
            return super.baseDAO.find(ql, paramMap);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Category> findCategorysByGroupId(String groupId,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findCategorysByGroupId(groupId).subList(firstIndex,firstIndex+fetchSize);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Category> findCategorysByGroupId(String groupId, String userId,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findCategorysByGroupId(groupId,userId).subList(firstIndex,firstIndex+fetchSize);

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Category> findCategorysByUserId(String userId,Integer firstIndex, Integer fetchSize) throws ErrException {
        try {
            return this.findCategorysByUserId(userId).subList(firstIndex,firstIndex+fetchSize);
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Category> findCategorysByGroupId(String groupId,Integer firstIndex) throws ErrException {
        try {
            List<Category> categoryList =  this.findCategorysByGroupId(groupId);
            return categoryList.subList(firstIndex,categoryList.size());
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Category> findCategorysByGroupId(String groupId, String userId,Integer firstIndex) throws ErrException {
        try {

            List<Category> categoryList = this.findCategorysByGroupId(groupId,userId);
            return categoryList.subList(firstIndex,categoryList.size());

        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }

    public List<Category> findCategorysByUserId(String userId,Integer firstIndex) throws ErrException {
        try {
            List<Category> categoryList = this.findCategorysByUserId(userId);
            return categoryList.subList(firstIndex,categoryList.size());
        } catch (Exception ex) {
            throw new ErrException(ErrCode.QUERY_FAILED);
        }
    }
}
